//
//  AppADView.m
//  suRead
//
//  Created by 李 诗洋 on 15/11/19.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "AppADView.h"
#import "UIImageView+WebCache.h"
#import "UIControl+Blocks.h"
#import <SDWebImage.h>

#define kDeviceWidth [UIScreen mainScreen].bounds.size.width
#define KDeviceHeight [UIScreen mainScreen].bounds.size.height

@interface AppADView()

@property (nonatomic,strong) UIButton *button;

@end

@implementation AppADView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.adImageview = [UIImageView new];
        [self.adImageview setFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight)];
        [self addSubview:self.adImageview];
        
        UIImage *adImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:@"adImage"];
        
        if (adImage) {
            self.adImageview.image = adImage;
        }
        
        UIImageView *img= [UIImageView new];
        [img setFrame:CGRectMake(0, KDeviceHeight-77, kDeviceWidth, 77)];
        [img setImage:[UIImage imageNamed:@"load_ad"]];
        [self addSubview:img];
        
        __weak typeof(self) weak_self = self;
        
        self.button = [[UIButton alloc] initWithFrame:CGRectMake(kDeviceWidth - 15 - 42, KDeviceHeight-77 + (77 / 2 - 20 / 2), 42, 20)];
        self.button.backgroundColor = [UIColor darkGrayColor];
        [self.button setTitle:@"跳过" forState:UIControlStateNormal];
        [self addSubview:self.button];
        [self.button addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
            [weak_self removeFromSuperview];
        }];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
    }
    return self;
}


@end
